package de.laures.cewolf.cpp;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;

import de.laures.cewolf.ChartPostProcessor;
import de.laures.cewolf.util.ChartUtils;

public class RangeProcessor implements ChartPostProcessor, Serializable {

	@Override
	public void processChart(Object chart, Map params) {
		JFreeChart localChart = (JFreeChart) chart;
		Plot plot = localChart.getPlot();
		if (plot instanceof XYPlot) {

			List<NumberAxis> list = getPlotDomainAxis(plot);

			String from = (String) params.get("from");
			String to = (String) params.get("to");
			if (from != null && to != null) {
				for (NumberAxis axis : list) {
					axis.setRange(Double.valueOf(from), Double.valueOf(to));
				}
			}
		}
	}

	private List<NumberAxis> getPlotDomainAxis(Plot plot) {
		List<NumberAxis> list = new LinkedList<NumberAxis>();
		List<Plot> plots = ChartUtils.getPlots(plot);
		for (Plot p : plots) {
			if (p instanceof XYPlot) {
				if (((XYPlot) p).getDomainAxis() instanceof NumberAxis) {
					list.add((NumberAxis) ((XYPlot) p).getDomainAxis());
				}
			}
		}
		return list;

	}
}
